package com.lenovo.manufacture.module;

import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.annotation.Layout;

import butterknife.BindView;
import butterknife.OnClick;

public class Module2 extends BaseActivity {

    @BindView(R.id.iv_snow)
    ImageView ivSnow;
    @BindView(R.id.iv_sun)
    ImageView ivSun;
    @BindView(R.id.btn_snow)
    Button btnSnow;
    @BindView(R.id.btn_sun)
    Button btnSun;
    @BindView(R.id.ib_switch)
    ImageButton ibSwitch;

    @Layout(value = R.layout.activity_module2, title = "工厂空调")
    protected void init() {
        async(apiService.getInfo(param.add("id", 1).build()), data -> refresh(data.getData().get(0).get("acSwitch")));
    }

    private void refresh(String acSwitch) {
        int snow = R.drawable.snow_off;
        int sun = R.drawable.sun_off;
        int ac = R.drawable.switch_off;
        switch (acSwitch) {
            case "1":
                snow = R.drawable.snow_on;
                ac = R.drawable.switch_on;
                break;
            case "2":
                sun = R.drawable.sun_on;
                ac = R.drawable.switch_on;
                break;
        }
        ivSnow.setImageResource(snow);
        ivSun.setImageResource(sun);
        ibSwitch.setImageResource(ac);
    }

    @OnClick({R.id.btn_snow, R.id.btn_sun, R.id.ib_switch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_snow:
                json.put("acSwitch", 1);
                break;
            case R.id.btn_sun:
                json.put("acSwitch", 2);
                break;
            case R.id.ib_switch:
                json.put("acSwitch", 0);
                break;
        }

        async(apiService.updateAcSwitch(json), data -> {
            String acSwitch = data.getData().get(0).get("acSwitch");
            refresh(acSwitch);
            String status = "关闭";
            switch (acSwitch) {
                case "1":
                    status = "冷风";
                    break;
                case "2":
                    status = "暖风";
                    break;
            }
            showMsg("切换成功，当前空调状态为：" + status);
        });
    }
}
